package les.projeto;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;

public class Import {

	private String fileChosen;
	private Context context;
	
	public Import(Context context, String fileChosen){
		this.fileChosen = fileChosen;
		this.context = context;
	}
	
	public void readFile() {
		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader(fileChosen));
	        String str;
	        ArrayList<String> data = new ArrayList<String>();
	        while (in.ready()) {
	            str = in.readLine();
	            data.add(str);
	            System.out.println(str);
	        }
	        attTeachers(data.get(0));
	        attActivities(data.get(1));
	        attLocal(data.get(2));
	        attSubjects(data.get(2));
	        attSemester(data.get(2));
	        
		} catch (FileNotFoundException e) {
			System.out.println("ERRO:" + e.getMessage());
		} catch (IOException e) {
			System.out.println("ERRO: " + e.getMessage());
		}
	}
	
	private void attSemester(String semesters) {
		if(semesters.equals("-"))
			return;
		String[] semesterData = semesters.split(" ");
		for (int i = 0; i < semesterData.length; i++) {
			String teacheraux = semesterData[i].replace(".", " ");
			String[] semester = teacheraux.split(" ");
			BD bd = new BD();
			bd.insertSemester(context, semester[0], semester[1], semester[2]);
		}
	}

	private void attSubjects(String subjects) {
		if(subjects.equals("-"))
			return;
		String[] subjectsData = subjects.split(" ");
		for (int i = 0; i < subjectsData.length; i++) {
			String subjectsaux = subjectsData[i].replace(".", " ");
			String[] subject = subjectsaux.split(" ");
			BD bd = new BD();
			bd.insertSubject(context, subject[0], subject[1], subject[2], subject[3], subject[4], Integer.valueOf(subject[5]), Integer.valueOf(subject[6]));
		}	
	}

	private void attLocal(String locals) {
		if(locals.equals("-"))
			return;
		String[] localsData = locals.split(" ");
		for (int i = 0; i < localsData.length; i++) {
			String localsaux = localsData[i].replace(".", " ");
			String[] local = localsaux.split(" ");
			BD bd = new BD();
			bd.insertLocal(context, local[0], local[1], local[2]);
		}
	}

	private void attTeachers(String teachers){
		if(teachers.equals("-"))
			return;
		String[] teachersData = teachers.split(" ");
		for (int i = 0; i < teachersData.length; i++) {
			String teacheraux = teachersData[i].replace(".", " ");
			String[] teacher = teacheraux.split(" ");
			BD bd = new BD();
			bd.insertTeacher(context, teacher[0], teacher[1], teacher[2]);
		}
	}
	
	private void attActivities(String activities){
		if(activities.equals("-"))
			return;
		String[] activitiesData = activities.split(" ");
		for (int i = 0; i < activitiesData.length; i++) {
			String activitiesaux = activitiesData[i].replace(".", " ");
			String[] activity = activitiesaux.split(" ");
			BD bd = new BD();
			bd.insertActivity(context, activity[0], activity[1], activity[2], activity[3]);
		}
	}
}
